package cn.edu.scau.cmi.qiuzeyang.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import cn.edu.scau.cmi.qiuzeyang.domain.Teacher;

public class TeacherDao extends HibernateSessionImpl{

	public static final String NAME = "name";

	public void save(Teacher transientInstance) {
		Session session = getSession();
		Transaction transaction = session.beginTransaction();
		session.save(transientInstance);
		transaction.commit();
		session.close();
	}

	public void delete(Teacher persistentInstance) {
		Session session = getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(persistentInstance);
		transaction.commit();
		session.close();
	}

	public void update(Teacher Teacher) {
		Session session = getSession();
		Transaction transaction = session.beginTransaction();
		session.update(Teacher);
		transaction.commit();
		session.close();
	}

	public Teacher findById(Long id) {
		Session session = getSession();
		Teacher Teacher = (Teacher) session.get("cn.edu.scau.cmi.qiuzeyang.hibernate.domain.Teacher", id);
		session.close();
		return Teacher;
	}

	public List<Teacher> findByProperty(String propertyName, Object value) {
		Session session = getSession();
		String queryString = "select * from Teacher where " + propertyName + " = ?";
		Query queryObject = session.createSQLQuery(queryString).addEntity(Teacher.class);
		queryObject.setParameter(0, value);
		List<Teacher> list = queryObject.list();
		session.close();
		return list;
	}

	public List<Teacher> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Teacher> findAll() {
		Session session = getSession();
		String queryString = "select * from Teacher";
		Query queryObject = session.createSQLQuery(queryString).addEntity(Teacher.class);
		List<Teacher> list = queryObject.list();
		session.close();
		return list;
	}
}
